package com.hust.chatvedio.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.hust.chatvedio.Entity.FriendEntity;
import com.hust.chatvedio.R;

import java.util.List;

public class FriendViewAdapter extends BaseAdapter {
    //ctrl+I 实现需要实现的方法
    //FriendEntity 对象的数组
    private List<FriendEntity> mData;
    //定义上下文
    private Context mCtx;
    //布局动态生成器
    private LayoutInflater layoutInflater;
    //构造函数
    public FriendViewAdapter(List<FriendEntity> mData, Context mCtx) {
        this.mData = mData;
        this.mCtx = mCtx;
        layoutInflater = LayoutInflater.from(mCtx);
    }
    @Override
    public int getCount() {
        //返回数组长度
        return mData == null ? 0 : mData.size();
    }
    @Override
    public Object getItem(int position) {
        //获取 Music 对象
        return mData == null ? null : mData.get(position);
    }
    @Override
    public long getItemId(int position) {
        //获取子项的位置
        return position;
    }
    //获取子视图
    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        FriendEntity entity=mData.get(i);
        ViewHolder mViewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            mViewHolder=new ViewHolder(convertView);
            convertView.setTag(mViewHolder);
        }else {
            //alt+enter
            mViewHolder= (ViewHolder) convertView.getTag();
        }
        mViewHolder.mTvName.setText(entity.getName());
        mViewHolder.mTvContent.setText(entity.getNewMsg());
        Glide.with(convertView).load(entity.getAvatarUrl()).into(mViewHolder.roundedImageView);
        return convertView;
    }
    public static class ViewHolder {
        public View rootView;
        public TextView mTvName;
        public TextView mTvContent;
        private RoundedImageView roundedImageView;
        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mTvName = (TextView)
                    rootView.findViewById(R.id.tv_name);
            this.mTvContent = (TextView)
                    rootView.findViewById(R.id.tv_content);
            this.roundedImageView = (RoundedImageView)
                    rootView.findViewById(R.id.avatar);
        }
    }
}